package com.example.xbannertest;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.xbannertest.entity.TuchongEntity;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.stx.xhb.androidx.XBanner;
import com.stx.xhb.androidx.transformers.Transformer;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class ClipChildrenModeActivity extends AppCompatActivity {

    private XBanner banner;
    private XBanner banner2;
    private XBanner banner3;
    private XBanner banner4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化Fresco
        Fresco.initialize(ClipChildrenModeActivity.this);
        setContentView(R.layout.activity_clip_children_mode);
        banner = findViewById(R.id.banner);
        banner2 = findViewById(R.id.banner2);
        banner3 = findViewById(R.id.banner3);
        banner4 = findViewById(R.id.banner4);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtil.getScreenWidth(this) / 2);

        banner.setLayoutParams(layoutParams);
        banner2.setLayoutParams(layoutParams);
        banner2.setPageTransformer(Transformer.Default);
        banner3.setLayoutParams(layoutParams);
        banner4.setLayoutParams(layoutParams);

        initBanner(banner);
        initBanner(banner2);
        initBanner(banner3);
        initBanner(banner4);

        initData();
    }

    private void initBanner(XBanner banner) {
        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                LogUtils.i("click pops:" + position);
                ToastUtils.showShort("点击了第" + (position + 1) + "张图片");
            }
        });

        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                SimpleDraweeView draweeView = (SimpleDraweeView) view;
                TuchongEntity.FeedListBean.EntryBean listBean = (TuchongEntity.FeedListBean.EntryBean) model;
                String url = "https://photo.tuchong.com/" + listBean.getImages().get(0).getUser_id() + "/f/" + listBean.getImages().get(0).getImg_id() + ".jpg";
                draweeView.setImageURI(Uri.parse(url));
            }
        });
    }

    private void initData() {
        String url = "https://api.tuchong.com/2/wall-paper/app";
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtils.showShort("加载失败0");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        TuchongEntity advertiseEntity = new Gson().fromJson(response, TuchongEntity.class);
                        List<TuchongEntity.FeedListBean> others = advertiseEntity.getFeedList();
                        List<TuchongEntity.FeedListBean.EntryBean> data = new ArrayList<>();
                        for (int i = 0; i < others.size(); i++) {
                            TuchongEntity.FeedListBean feedListBean = others.get(i);
                            if ("post".equals(feedListBean.getType())) {
                                data.add(feedListBean.getEntry());
                            }
                        }
                        banner.setAutoPlayAble(data.size() > 1);
                        banner.setIsClipChildrenMode(true);
                        banner.setBannerData(R.layout.layout_fresco_imageview, data);

                        banner2.setAutoPlayAble(data.size() > 1);
                        banner2.setIsClipChildrenMode(true);
                        banner2.setBannerData(R.layout.layout_fresco_imageview, data);

                        banner3.setAutoPlayAble(data.size() > 1);
                        banner3.setIsClipChildrenMode(true);
                        banner3.setBannerData(R.layout.layout_fresco_imageview, data);

                        banner4.setAutoPlayAble(data.size() > 1);
                        banner4.setIsClipChildrenMode(true);
                        banner4.setBannerData(R.layout.layout_fresco_imageview, data);
                        banner4.getViewPager().setOffscreenPageLimit(1);
                    }
                });
    }
}